package io.xccit.system.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.xccit.common.result.AjaxResult;
import io.xccit.log.annotation.Log;
import io.xccit.log.enums.BusinessType;
import io.xccit.model.system.SysMenu;
import io.xccit.system.service.ISysMenuService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author xccit
 * @since 2023-06-26
 */
@Api(tags = "菜单接口")
@RestController
@RequestMapping("/api/system/sysMenu")
public class SysMenuController {

    @Resource
    private ISysMenuService sysMenuService;

    /**
     * 树形结构显示菜单列表
     * @return 菜单列表
     */
    @Log(title = "获取菜单列表")
    @PreAuthorize("hasAuthority('btn.sysMenu.list')")
    @ApiOperation("菜单列表")
    @GetMapping("/findNodes")
    public AjaxResult<List<SysMenu>> selectNodes() {
        List<SysMenu> nodes = sysMenuService.findNodes();
        return AjaxResult.ok(nodes);
    }

    /**
     * 获取所有菜单,前端过滤子菜单
     * @return
     */
    @Log(title = "获取所有菜单")
    @PreAuthorize("hasAuthority('btn.sysMenu.list')")
    @ApiOperation("获取所有菜单")
    @GetMapping("/selectParent")
    public AjaxResult<List<SysMenu>> selectParent(){
        return AjaxResult.ok(sysMenuService.selectParent());
    }

    @Log(title = "根据ID查询菜单")
    @PreAuthorize("hasAuthority('btn.sysMenu.list')")
    @ApiOperation("根据ID查询")
    @GetMapping("/get/{id}")
    public AjaxResult<SysMenu> selectMenuByID(@PathVariable Long id){
        SysMenu sysMenu = sysMenuService.getById(id);
        return AjaxResult.ok(sysMenu);
    }

    @Log(title = "添加菜单",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('btn.sysMenu.add')")
    @ApiOperation("菜单添加")
    @PostMapping("/save")
    public AjaxResult<String> saveMenu(@RequestBody SysMenu sysMenu){
        boolean save = sysMenuService.save(sysMenu);
        if (save){
            return AjaxResult.ok("添加成功");
        }else{
            return AjaxResult.fail("添加失败");
        }
    }

    @Log(title = "修改菜单",businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('btn.sysMenu.update')")
    @ApiOperation("修改菜单")
    @PutMapping("/update")
    public AjaxResult<String> updateMenu(@RequestBody SysMenu sysMenu){
        boolean updated = sysMenuService.updateById(sysMenu);
        if (updated){
            return AjaxResult.ok("修改成功");
        }else{
            return AjaxResult.fail("修改失败");
        }
    }

    @Log(title = "删除菜单",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('btn.sysMenu.remove')")
    @ApiOperation("删除菜单")
    @DeleteMapping("/remove/{id}")
    public AjaxResult<String> deleteMenu(@PathVariable Long id){
        boolean deleted = sysMenuService.removeMenuById(id);
        if (deleted){
            return AjaxResult.ok("删除成功");
        }else{
            return AjaxResult.fail("删除失败");
        }
    }
}

